/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alee.laf.tree;

import javax.swing.*;
import javax.swing.plaf.TreeUI;
import javax.swing.tree.TreeCellRenderer;

/**
 * Custom default tree cell renderer for WebLookAndFeel.
 *
 * @author Mikle Garin
 * @since 1.4
 */

public class WebTreeCellRenderer extends WebTreeElement implements TreeCellRenderer
{
    /**
     * Constructs default tree cell renderer.
     */
    public WebTreeCellRenderer ()
    {
        super ();
        setName ( "Tree.cellRenderer" );
        setForeground ( UIManager.getColor ( "Tree.textForeground" ) );
    }

    /**
     * Returns tree cell renderer component.
     *
     * @param tree       tree
     * @param value      cell value
     * @param isSelected whether cell is selected or not
     * @param expanded   whether cell is expanded or not
     * @param leaf       whether cell is leaf or not
     * @param row        cell row number
     * @param hasFocus   whether cell has focus or not
     * @return cell renderer component
     */
    public WebTreeElement getTreeCellRendererComponent ( JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf,
                                                         int row, boolean hasFocus )
    {
        final boolean enabled = tree.isEnabled ();

        // Visual settings
        setFont ( tree.getFont () );
        setEnabled ( enabled );

        // Icon
        if ( enabled )
        {
            setIcon ( leaf ? WebTreeUI.LEAF_ICON : ( tree.getModel ().getRoot () == value ? WebTreeUI.ROOT_ICON :
                    ( expanded ? WebTreeUI.OPEN_ICON : WebTreeUI.CLOSED_ICON ) ) );
        }
        else
        {
            setIcon ( leaf ? WebTreeUI.DISABLED_LEAF_ICON : ( tree.getModel ().getRoot () == value ? WebTreeUI.DISABLED_ROOT_ICON :
                    ( expanded ? WebTreeUI.DISABLED_OPEN_ICON : WebTreeUI.DISABLED_CLOSED_ICON ) ) );
        }

        // Text
        setText ( tree.convertValueToText ( value, isSelected, expanded, leaf, row, hasFocus ) );

        // Border
        TreeUI tui = tree.getUI ();
        int sw = tui instanceof WebTreeUI ? ( ( WebTreeUI ) tui ).getSelectionShadeWidth () : WebTreeStyle.selectionShadeWidth;
        setMargin ( sw + 2, sw + 2, sw + 2, sw + 4 );

        // Orientation
        setComponentOrientation ( tree.getComponentOrientation () );

        return this;
    }
}